import pymysql
import time


def DHT11():
    while 1:
        temperature = 24
        humidity = 55
        print("内部温度是： ", temperature, "内部湿度是： ", humidity)
        con = pymysql.connect(
            host='localhost',
            port=3306,
            user='root',
            password='!qAz2WsX3edc',
            db='test',
            charset='utf8'
        )
        cur = con.cursor()
        address = "DHT22"
        time1 = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time()))
        cur.executemany(
            "INSERT DHT20(address,time,temperature,humidity) VALUE(%s,%s,%s,%s)",
            [(address, time1, temperature, humidity)])
        con.commit()
        cur.close()
        time.sleep(60)


def DHT11_restart():
    time.sleep(61)
    while 1:
        con = pymysql.connect(
            host='localhost',
            port=3306,
            user='root',
            password='!qAz2WsX3edc',
            db='test',
            charset='utf8'
        )
        cur = con.cursor()
        cur.execute("select min(id) from DHT20 where time > DATE_ADD(now(),INTERVAL -5 MINUTE) ")
        min_id = cur.fetchone()[0]
        if min_id is None:
            print("error")
        con.commit()
        cur.close()
        time.sleep(60)


DHT11_restart()
